/** 
 
*/
import React, { PureComponent } from 'react';
import { SelectC, ModalC, DATA_ENTRY, SecondHeaderC, codeType, TenantC, SecuritiesCodeC } from '@/src/comps';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { message, Modal, Input } from 'antd';
import { connect } from 'dva';
import moment from 'moment';
import SPPITestItem from './SPPITestItem';
import configs from './configs';
import 'moment/locale/zh-cn';

const { TextArea } = Input;

const FormItemLayout = {
  labelCol: {
    xs: { span: 2, offset: 0 },
    sm: { span: 4, offset: 0 },
    md: { span: 4, offset: 0 },
    lg: { span: 4, offset: 0 },
    xl: { span: 3, offset: 0 },
    xxl: { span: 3, offset: 0 },
  },
  wrapperCol: {
    xs: { span: 22, offset: 0 },
    sm: { span: 20, offset: 0 },
    md: { span: 20, offset: 0 },
    lg: { span: 20, offset: 0 },
    xl: { span: 21, offset: 0 },
    xxl: { span: 21, offset: 0 },
  },
};

@Form.create()
class sppiTest extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      superMan: localStorage.getItem('antd-pro-superMan') == 'true',
      defaultTenant: localStorage.getItem('antd-pro-tenant'),
      tenant: undefined,
      xtpd: '',
      rgpd: undefined,
    };
  }

  componentDidMount() {
    this.props.onRef(this);
    // exchangeCode: row.exchangeCode,
    // tradingCode: row.tradingCode,
    const { exchangeCode, tradingCode, type, tenant } = this.props;
    if (exchangeCode && tradingCode && type == 2) {
      this.props.dispatch({
        type: 'sppiTestRecordM/getDetail',
        payload: {
          // ...payload,
          exchangeCode: exchangeCode,
          tradingCode: tradingCode,
          tenantId: tenant,
        },
        callback: (status, res) => {
          if (status == 'success') {
            const { id, ...formData } = res.data;
            this.props.form?.setFieldsValue(formData);
          }
        },
      });
    }
  }

  render() {
    const { type, form, exchangeCode, tradingCode, tenant, onSecurityChange } = this.props;
    const { getFieldDecorator, getFieldsValue } = form;
    const { xtpd, rgpd } = getFieldsValue();

    let queryItems = {};
    if (type == 1) {
      queryItems = { tenant: this.state.tenant ? this.state.tenant : tenant || this.state.defaultTenant };
    } else if (type == 2) {
      queryItems = { tenant: tenant || this.state.defaultTenant };
    } else if (type == 3) {
      queryItems = { tenant: this.state.defaultTenant };
    } else {
      queryItems = { tenant: null };
    }

    return (
      <div>
        {this.state.superMan && type == 1 && (
          <TenantC
            form={this.props.form}
            dataIndex='tenantId'
            width={300}
            allowClear={false}
            initialValue={tenant}
            formItemLayout={FormItemLayout}
            onChange={(e) => this.setState({ tenant: e.key })}
          />
        )}
        <SecuritiesCodeC // 证券代码
          title='证券代码'
          extraLayout={FormItemLayout}
          form={this.props.form}
          width={300}
          initialValue={exchangeCode && tradingCode ? `${exchangeCode}-${tradingCode}` : undefined}
          dataIndex='securityId'
          codeType={codeType.sppi}
          queryItems={queryItems}
          onChange={(e) => {
            onSecurityChange(e);
          }}
          disabled={type == '1' ? false : true}
        />
        <div>
          {configs.map((row, index) => {
            return <SPPITestItem options={row} key={index} form={form}></SPPITestItem>;
          })}
        </div>
        <div>
          <Form.Item label={'系统判断'} {...FormItemLayout}>
            {getFieldDecorator('xtpd', {})(<span style={{ marginLeft: '11px' }}>{xtpd == '1' ? '通过' : xtpd == '2' ? '不通过' : '-'}</span>)}
          </Form.Item>
          <SelectC
            form={form}
            formItemLayout={FormItemLayout}
            dataIndex={'rgpd'}
            title='人工判断'
            selectWidth={200}
            // initialValue={rgpd}
            dataSource={[
              { value: '1', title: '通过' },
              { value: '2', title: '不通过' },
            ]}
          />
          <Form.Item label='备注' {...FormItemLayout}>
            {getFieldDecorator('bz', {})(<TextArea placeholder='' />)}
          </Form.Item>
        </div>
      </div>
    );
  }
}
export default connect(({ sppiTestRecordM, loading }) => ({
  sppiTestRecordM,
  loading,
}))(sppiTest);
